package cn.iocoder.yudao.module.dify.controller.admin.order;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.dify.controller.admin.order.vo.TransCreateReqVO;
import cn.iocoder.yudao.module.dify.dal.dataobject.translationtaskrecords.TranslationTaskRecordsDO;
import cn.iocoder.yudao.module.dify.dal.mysql.translationtaskrecords.TranslationTaskRecordsMapper;
import cn.iocoder.yudao.module.dify.service.translationtaskrecords.TranslationTaskRecordsService;
import cn.iocoder.yudao.module.pay.api.notify.dto.PayOrderNotifyReqDTO;
import cn.iocoder.yudao.module.pay.api.order.PayOrderApi;
import cn.iocoder.yudao.module.pay.api.order.dto.PayOrderCreateReqDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.time.Duration;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.addTime;
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.dify.enums.ErrorCodeConstants.TRANSLATION_TASK_RECORDS_NOT_EXISTS;

@Tag(name = "管理后台 - 支付订单")
@RestController
@RequestMapping("/dify/order")
@Validated
public class OrderController {
    @Resource
    PayOrderApi payOrderService;
    @Resource
    private TranslationTaskRecordsService translationTaskRecordsService;
    @Resource
    private TranslationTaskRecordsMapper translationTaskRecordsMapper;
    @PostMapping("/create")
    @Operation(summary = "创建对应翻译的订单")
    @Transactional
    public CommonResult<Long> createDemoOrder(@Valid @RequestBody TransCreateReqVO createReqVO) {
       //校验该任务是否存在
        Long taskId = createReqVO.getTaskId();
        TranslationTaskRecordsDO translationTaskRecords = translationTaskRecordsService.getTranslationTaskRecords(taskId);
        if (ObjectUtil.isNull(translationTaskRecords)){
            throw exception(TRANSLATION_TASK_RECORDS_NOT_EXISTS);
        }
        // 2.1 创建支付单
        Long payOrderId = payOrderService.createOrder(new PayOrderCreateReqDTO()
                .setAppKey("trans_pay").setUserIp(getClientIP())
                .setMerchantOrderId(String.valueOf(createReqVO.getTaskId())) // 业务的订单编号
                .setSubject("翻译任务支付").setBody("")
                .setPrice(createReqVO.getPayPrice())
                .setAppKey("trans_pay")
                .setExpireTime(addTime(Duration.ofHours(2L))));
        translationTaskRecords.setOrderId(payOrderId);
        translationTaskRecordsMapper.updateById(translationTaskRecords);
        return success(payOrderId);
    }


    @PostMapping("/update-paid")
    @Operation(summary = "更新翻译订单为已支付") // 由 pay-module 支付服务，进行回调，可见 PayNotifyJob
    @PermitAll // 无需登录，安全由 PayDemoOrderService 内部校验实现
    public CommonResult<Boolean> updateDemoOrderPaid(@RequestBody PayOrderNotifyReqDTO notifyReqDTO) {
       //TODO:未完成翻译任务的状态变更
        return success(true);
    }
}
